import Vue from 'vue'
import Router from 'vue-router'
import MainNew from '@/page/main_new'
// 登录页
import Login from '@/page/login'
// 一级分类管理
import ManageType from '@/page/manage_type'
// 二级分类管理
import ManageClass from '@/page/manage_class'
// 号码管理
import ManageTel from '@/page/manage_tel'
import FormCom from '@/components/form_com'
import TableCom from '@/components/table_com'
// 用户列表
import User from '@/page/user'

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
Vue.use(Router)


let router = new Router({
  routes: [
    { path: "/formcom", component: FormCom },
    { 
      path: '/manage', component: MainNew, name: "MainNew",
      children:[
        { path: "formcom", component: FormCom },
        { path: "tablecom", component: TableCom },
        { path: "managetype", component: ManageType },
        { path: "manageclass", component: ManageClass },
        { path: "managetel", component: ManageTel },
        { path: "user", component: User },
      ]
    },
    { path: '/login', component: Login, name: "login" },
    { path: '*', redirect: '/manage' } /*默认跳转路由*/
  ] 
})

// 访问之前，都检查下是否登录了
router.beforeEach((to, from, next) => {
  // debugger;
  if (to.path.startsWith('/login')) {
    window.sessionStorage.removeItem('access-token')
    next()
  } else {
    let token = window.sessionStorage.getItem('access-token')
    if (!token) {
        next({ path: '/login' })
    } else {
        next()
    }
  }
})

export default router
